package top.oylan.mp.service;

import org.springframework.stereotype.Service;
import top.oylan.mp.entity.Menu;
import top.oylan.mp.entity.RoleUser;
import top.oylan.mp.entity.User;
import top.oylan.mp.entity.example.RoleUserExample;
import top.oylan.mp.entity.example.UserExample;
import top.oylan.mp.entity.vo.Admin;
import top.oylan.mp.mapper.RoleUserMapper;
import top.oylan.mp.mapper.UserMapper;
import top.oylan.mp.utils.Pager;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Resource
    UserMapper userMapper;

    public User findByAccount(String account) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(account);
        return userMapper.findUser(userExample);
    }

    public List<User> selectByPage(Pager pager) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        User condition = (User) pager.getCondition();
        if (condition.getType() != null) {
            criteria.andTypeEqualTo(condition.getType());
        }
        if (condition.getName() != null && !condition.getName().equals("")) {
            criteria.andNameLike("%" + condition.getName() + "%");
        }
        //  criteria 赋条件
        pager.setTotalRow((int) userMapper.countByExample(example));
        int offset = pager.getRowSize() * (pager.getCurrentPage() - 1);
        int length = pager.getRowSize();
        example.setOffset(offset);
        example.setLength(length);
        List<User> resultList = userMapper.selectByExample(example);
        pager.setList(resultList);
        return resultList;
    }

    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    public void addUser(User user) {
        userMapper.insertSelective(user);
    }

    public void deleteUser(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Resource
    RoleUserMapper roleUserMapper;

    /**
     * 更新管理员
     **/
    public void updateAdmin(Admin admin) {
        userMapper.updateByPrimaryKeySelective(admin);
        RoleUserExample example = new RoleUserExample();
        RoleUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(admin.getId());
        roleUserMapper.deleteByExample(example);
        saveRoles(admin.getId(), admin.getRole());
    }

    /**
     * 添加管理员
     **/
    public void insetAdmin(Admin admin) {
        admin.setType((byte) 1);
        admin.setCreateTime(new Date());
        userMapper.insertSelective(admin);
        saveRoles(admin.getId(), admin.getRole());
    }

    // 分配管理员角色
    private void saveRoles(Integer id, List<Integer> role) {
        for (int roleId : role) {
            RoleUser roleUser = new RoleUser();
            roleUser.setRoleId(roleId);
            roleUser.setUserId(id);
            roleUserMapper.insertSelective(roleUser);
        }
    }

    public List<Admin> selectAdminByPage(Pager<Admin> pager) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        User condition = (User) pager.getCondition();
        if (condition.getName() != null && !condition.getName().equals("")) {
            criteria.andNameLike("%" + condition.getName() + "%");
        }
        if (condition.getType() != null) {
            criteria.andTypeEqualTo(condition.getType());
        }
        pager.setTotalRow((int) userMapper.countByExample(example));

        return userMapper.selectAdminByPager(pager);
    }

    /**
     * 授权菜单
     */
    public List<Menu> selectPromisedMenus(Integer userId) {
        return userMapper.selectPromisedMenus(userId);
    }
}
